<template>
  <div class="mod-demo-echarts">
    <!-- <el-alert
      title="提示："
      type="warning"
      :closable="false">
      <div slot-scope="description">
        <p class="el-alert__description">1. 此Demo只提供ECharts官方使用文档，入门部署和体验功能。具体使用请参考：http://echarts.baidu.com/index.html</p>
      </div>
    </el-alert> -->

    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <div id="J_chartLineBox" class="chart-box"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="J_chartPieBox1" class="chart-box"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="J_chartPieBox2" class="chart-box"></div>
        </el-card>
      </el-col>

    </el-row>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  data() {
    return {
      chartLine: null,
      chartBar: null,
      chartPie: null,
      chartScatter: null,
    }
  },
  mounted() {
    this.initChartLine()
    this.initChartPie1()
    this.initChartPie2()
  },
  activated() {
    // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
    if (this.chartLine) {
      this.chartLine.resize()
    }
    if (this.chartBar) {
      this.chartBar.resize()
    }
    if (this.chartPie) {
      this.chartPie.resize()
    }
    if (this.chartScatter) {
      this.chartScatter.resize()
    }
  },
  methods: {
    // 折线图
    initChartLine() {
      var arr1 = new Array(7);
      let arr2 = new Array(7);
      this.$http({
        url: "http://localhost:88/api/film/count/countFilmData",
        method: "get"
      }).then(result => {
        let finalResult = result.data.data;
        let i = 0;
        finalResult.forEach(item => {
          arr1[i] = item.dayPlay;
          arr2[i] = item.countDate;
          i++;
        })
        var option = {
          'title': {
            'text': '电影播放统计'
          },
          'tooltip': {
          },
          'grid': {
            'left': '3%',
            'right': '4%',
            'bottom': '3%',
            'containLabel': true
          },
          'toolbox': {
            'feature': {
              'saveAsImage': {}
            }
          },
          'xAxis': {
            'type': 'category',
            'boundaryGap': false,
            'data': arr2
          },
          'yAxis': {
            'type': 'value',
            minInterval: 1  // 将纵坐标设置为只显示整数
          },
          'series': [
            {
              'name': '邮件营销',
              'type': 'line',
              'stack': '总量',
              'data': arr1
            },
          ]
        }
        this.chartLine = echarts.init(document.getElementById('J_chartLineBox'))
        this.chartLine.setOption(option)
        window.addEventListener('resize', () => {
          this.chartLine.resize()
        })
      })
    },
    // 饼状图
    initChartPie1() {
      this.$http({
        url: "http://localhost:88/api/film/count/countLabelData",
        method: "get"
      }).then(result => {
        let finalResult = result.data.data;
        let labels = [];
        let counts = [];

        let finalData = []
        
        let i = 0;
        finalResult.forEach(item => {

          var obj = {
            value: item.count,
            name: item.title
          }
          finalData[i] = obj;

          i ++;
        })


        var option = {
          backgroundColor: '#2c343c',
          title: {
            text: '资源标签分布',
            left: 'center',
            top: 20,
            textStyle: {
              color: '#ccc'
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          visualMap: {
            show: false,
            min: 80,
            max: 600,
            inRange: {
              colorLightness: [0, 1]
            }
          },
          series: [
            {
              name: '标签',
              type: 'pie',
              radius: '55%',
              center: ['50%', '50%'],
              data: finalData.sort(function (a, b) { return a.value - b.value }),
              roseType: 'radius',
              label: {
                normal: {
                  textStyle: {
                    color: 'rgba(255, 255, 255, 0.3)'
                  }
                }
              },
              labelLine: {
                normal: {
                  lineStyle: {
                    color: 'rgba(255, 255, 255, 0.3)'
                  },
                  smooth: 0.2,
                  length: 10,
                  length2: 20
                }
              },
              itemStyle: {
                normal: {
                  color: '#c23531',
                  shadowBlur: 200,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              },
              animationType: 'scale',
              animationEasing: 'elasticOut',
              animationDelay: function (idx) {
                return Math.random() * 200
              }
            }
          ]
        }
        this.chartPie = echarts.init(document.getElementById('J_chartPieBox1'))
        this.chartPie.setOption(option)
        window.addEventListener('resize', () => {
          this.chartPie.resize()
        })
      })

    },
    initChartPie2() {

      this.$http({
        url: "http://localhost:88/api/film/count/countData",
        method: "get"
      }).then(result => {
        let finalResult = result.data.data;
        
        let finalData = [];

        let i = 0;
        finalResult.forEach(item => {
          let obj = {
            value: item.count,
            name: item.filmName
          }
          finalData[i] = obj;
          i ++;
        })

        var option = {
        backgroundColor: '#2c343c',
        title: {
          text: '电影资源分布',
          left: 'center',
          top: 20,
          textStyle: {
            color: '#ccc'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        visualMap: {
          show: false,
          min: 80,
          max: 600,
          inRange: {
            colorLightness: [0, 1]
          }
        },
        series: [
          {
            name: '资源',
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            data: finalData.sort(function (a, b) { return a.value - b.value }),
            roseType: 'radius',
            label: {
              normal: {
                textStyle: {
                  color: 'rgba(255, 255, 255, 0.3)'
                }
              }
            },
            labelLine: {
              normal: {
                lineStyle: {
                  color: 'rgba(255, 255, 255, 0.3)'
                },
                smooth: 0.2,
                length: 10,
                length2: 20
              }
            },
            itemStyle: {
              normal: {
                color: '#c23531',
                shadowBlur: 200,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      }
      this.chartPie = echarts.init(document.getElementById('J_chartPieBox2'))
      this.chartPie.setOption(option)
      window.addEventListener('resize', () => {
        this.chartPie.resize()
      })

      })
    },
  }
}
</script>

<style lang="scss">
.mod-demo-echarts {
  >.el-alert {
    margin-bottom: 10px;
  }

  >.el-row {
    margin-top: -10px;
    margin-bottom: -10px;

    .el-col {
      padding-top: 10px;
      padding-bottom: 10px;
    }
  }

  .chart-box {
    min-height: 400px;
  }
}</style>
